<?php
//统计前天注册，昨天登录的用户
$plantform = $argv[1];
$countday = $argv[2];
$countLoginDay = $argv[3];
if ($plantform){
    $regfrom= $plantform;
}else {
    $regfrom= 'kc';
}
if (!$countday){
    $countday = 2;
}
if (!$countLoginDay){
    $countLoginDay=$countday-1;
}
if ($countLoginDay>=$countday){
    echo '登陆的天数必须小于注册的天数，例如，要看昨天登录的用户，注册日期必须是前天或前天以前';
    die();
}
$filename = 'countSaveUser.txt';
set_time_limit(-1);
include 'config.php';

$befYestoday = date("Ymd",strtotime("-".$countday." days"));#几天前注册
$yestoday = date("Ymd",strtotime("-".$countLoginDay." days"));#昨天登录的

$tmpTableUser = 't_user_'.$befYestoday;#前天注册的用户表
$tmpTableLogLogin = 'log_login_'.$yestoday;#昨天登录的日志表
$tmpTableBefLogLogin = 'log_login_'.$befYestoday;#前天登录的日志表

$befYestodayStime = date("Y-m-d",strtotime("-".$countday." days"))." 00:00:00";#几天前开始时间
$befYestodayEtime = date("Y-m-d",strtotime("-".$countday." days"))." 23:59:59";#几天前结束时间

$yestodayStime = date("Y-m-d",strtotime("-".$countLoginDay." days"))." 00:00:00";#登录开始时间
$yestodayEtime = date("Y-m-d",strtotime("-".$countLoginDay." days"))." 23:59:59";#登录结束时间

$sql = "DROP TABLE IF EXISTS `$tmpTableUser`;";
$db->query($sql);
$sql = "CREATE TABLE $tmpTableUser LIKE t_user ";
$db->query($sql);
$sql = "INSERT INTO $tmpTableUser SELECT * FROM t_user WHERE regtime >= '$befYestodayStime' AND regtime<= '$befYestodayEtime'";#插入前天注册的用户
$db->query($sql);

$sql = "DROP TABLE IF EXISTS `$tmpTableLogLogin`;";
$db->query($sql);
$sql = "CREATE TABLE $tmpTableLogLogin LIKE t_log_login ";
$db->query($sql);
$sql = "INSERT INTO $tmpTableLogLogin SELECT * FROM t_log_login WHERE `s_time` >= '$yestodayStime' AND `s_time`<= '$yestodayEtime'";#插入昨天登录的用户
$db->query($sql);

$sql = "DROP TABLE IF EXISTS `$tmpTableBefLogLogin`;";
$db->query($sql);
$sql = "CREATE TABLE $tmpTableBefLogLogin LIKE t_log_login ";
$db->query($sql);
$sql = "INSERT INTO $tmpTableBefLogLogin SELECT * FROM t_log_login WHERE `s_time` >= '$befYestodayStime' AND `s_time`<= '$befYestodayEtime'";#插入前天登录的用户
$db->query($sql);


$sql = "SELECT * FROM $tmpTableUser WHERE regfrom = '$regfrom'";
$allUser = $db->getAll($sql);
$counts = count($allUser);//总计人数
$countEffect=0;//有效注册用户，填写资料的数
$countLogin = 0;//登录的用户数
$countFinishNewUser = 0;//走完新手流程
$countNickname=0;//填写昵称的
$countIn3min = 0;
$countOut3min = 0;
$countFinishAfter = 0;//次日之后完成新手
foreach ($allUser as $k=>$v){
    $uid = $v['uid'];   
    if ($v['province']!=''){
        $countEffect++;
    }
    if ($v['nickname']!=''){
        $countNickname++;
    }
    //["reg_sex", "reg_basic_info", "first_buy_goods", "first_get_my_home_gold", "first_bar_ask", "first_enter_other_home", "first_bar_accept", "first_bar_talking"]
    if (str_replace('first_bar_ask','',$v['reg_step'])==$v['reg_step']){
        
        $sql = "SELECT * FROM  $tmpTableBefLogLogin WHERE uid = $uid AND sumtime >1 AND nickname!='' ORDER BY id LIMIT 1";
        $row = $db->getRow($sql);
        if ($row){
            $countFinishNewUser++;
            if ($row['sumtime']<180){
                $countIn3min++;
            }else {
                $countOut3min++;
                
            }
            
        }else{
            echo $uid."\r\n";
            $countFinishAfter++;
        }
    }
    $sql = "SELECT uid FROM  $tmpTableLogLogin WHERE uid = $uid ";
    $row = $db->getRow($sql);
    if ($row){
        $countLogin++;
    }
    
    
}

$nowtime = date("Y-m-d H:i:s");	//统计时间
$jirinei = $countday-$countLoginDay;	//几日内
$regtime = date("Y-m-d",strtotime("-".$countday." days")); //注册日期
$countLoginDay = date("Y-m-d",strtotime("-".$countLoginDay." days"));	//登录日期
$data = $nowtime.'|'.$regfrom.'|'.$jirinei.'日内|'.$regtime.'|'.$counts //注册总人数:
.'|'.$countNickname//填写昵称人数
.'|'.$countEffect//有效注册人数
.'|'.$countFinishNewUser//走完新手流程
.'|'.$countFinishAfter//走完新手流程
.'|'.$countIn3min//呆上三分钟内
.'|'.$countOut3min//呆上三分钟以上
.'|'.$countLoginDay
.'|'.$countLogin."\r\n";//登录人数
echo $data;
file_put_contents($filename, $data,FILE_APPEND);


/* 2012-02-29 *** wudi *** START ***
/* 将统计好的数据写入到数据表 */
$sql = sprintf("insert into t_log_login_count(optime, regfrom, countday, regtime, counts, countNickname, countEffect, countFinishNewUser, countFinishAfter,countIn3min, countOut3min, countLoginDay, countLogin, opflag) values ('%s','%s', %d, '%s', %d, %d, %d, %d, %d, %d, %d, '%s', %d, 'countSaveUser.php')", $nowtime, $regfrom, $jirinei, $counts, $countNickname, $countEffect, $countFinishNewUser, $countFinishNewUser, $countFinishAfter, $countIn3min, $countOut3min, $countLoginDay, $countLogin);
echo $sql;
$db->query($sql);
/* 2012-02-29 *** wudi *** END */


$sql = "DROP TABLE $tmpTableUser";
$db->query($sql);
$sql = "DROP TABLE $tmpTableLogLogin";
$db->query($sql);
$sql = "DROP TABLE $tmpTableBefLogLogin";
$db->query($sql);
?>